class Admin::ItemCategoriesController < Admin::BaseController

	def index
		@cat1 = ItemCategory.cat1
    unless params[:cid1].blank?
      @cat2 = ItemCategory.period(params[:cid1], "1")
    end
    
    unless params[:cid2].blank?
      @cat3 = ItemCategory.period(params[:cid2], "1")
    end
	end
	
	def edit
		@category = ItemCategory.find(params[:id])
	end
	
	def update
		@category = ItemCategory.find(params[:id])
		permalink = params[:item_category][:permalink].downcase
		if !permalink.to_s.blank? && ItemCategory.find_by_permalink(permalink)
			flash[:error] = "permalink 已存在!"
      render :action => "edit"
      return
		end
		
		if @category.update_attributes(params[:item_category])
			Item.update_all("cat1_name = '#{@category.view_name}'", "cat1_id = #{@category.t_cid}") if @category.cat_level == 0
			Item.update_all("cat2_name = '#{@category.view_name}'", "cat2_id = #{@category.t_cid}") if @category.cat_level == 1
			Item.update_all("cat1_name = '#{@category.view_name}'", "cat3_id = #{@category.t_cid}") if @category.cat_level == 2
			flash[:notice] = "动态更新成功"
      redirect_to '/admin/item_categories'
    else
      flash[:error] = "动态更新失败"
      render :action => "edit"
    end
	end
	
	def destroy
		@category = ItemCategory.find(params[:id])
    @category.custom_delete
		redirect_to '/admin/item_categories'
	end
end
